package com.pignest.hotel.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.type.BooleanType;
import org.hibernate.type.Type;

import com.pignest.hotel.beans.Duty;
import com.pignest.hotel.beans.Role;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.dao.UserDao;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.system.L;

@SuppressWarnings("unchecked")
public class UserService extends BaseService {

	private UserDao userDao;
	private RoleService roleService;
	
	public List<User> listAllUsers(){
		return query(User.class, "reserve=?", 0, 0, new Object[] {Boolean.FALSE}, new Type[] {new BooleanType()});
	}

	public User getUserByWorkNo(String workNo) {
		return userDao.getUserByWorkNo(workNo);
	}
	
	public User getUserByCardNumber(String cardNumber) {
		return userDao.getUserByCardNumber(cardNumber);
	}

	/*通过卡序列号获得卡号**/
	public String getCardNumberBySNR(String snr) {
		return userDao.getCardNumberBySnr(snr);
	}
	
	public boolean isSuperUserInitialized() {
		return getUserByWorkNo(Config.getSuperUserWorkNumber()) != null;
	}
	
	public boolean insertSuperUser() {
		if (isSuperUserInitialized())
			return false;
		User user = new User("Hotel7", Config.getSuperUserWorkNumber(), Config.getDefaultPassword(), null);
		user.setReserve(true);
		insert(user);
		return true;
	}
	
	public List<User> findUsers(String keyword, boolean enable, String roleId) {
		Role role = roleId != null ? get(Role.class, roleId) : null;
		return userDao.findUsers(keyword, enable, role);
	}
	
	public void deleteUsers(List<String> userIds){
		if (userIds == null)
			return;
		for (String userId : userIds) {
			deleteUser(userId);
		}
	}
	
	public void resetPasswords(List<String> userIds) {
		if (userIds == null)
			return;
		List<User> users = new ArrayList<User>();
		for (String userId : userIds) {
			User user = get(User.class, userId);
			if (user != null) {
				user.setPassword(null);
				users.add(user);
			}
		}
		if (!users.isEmpty()) {
			saveAll(users);
			L.update(users, "重置密码");
		}
	}
	
	public void deleteUser(String id) {
		User user = get(User.class, id);
		roleService.clearUserRoles(user);
		delete(user);
	}
	
	public void insertUser(User user, List<String> roleIds) {
		user.setJoinTime(new Date());
		insert(user);
		roleService.saveUserRole(user, roleIds);
	}
	
	public void updateUser(User user, List<String> roleIds) {
		update(user);
		roleService.saveUserRole(user, roleIds);
	}
	
	public Duty getDuty(User user) {
		if (user == null)
			return null;
		List<Duty> duties = query(Duty.class, "user=? and status!=?", new Object[] {user, Integer.valueOf(Duty.STATUS_OVER)}, null);
		return uniqueResult(duties);
	}
	
	public List<Duty> getCurrentDuties() {
		List<Duty> duties = query(Duty.class, "status!=?", new Object[] {Integer.valueOf(Duty.STATUS_OVER)}, null);
		return duties;
	}

	///////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * 判断除了当前记录外是否存在其它的账号
	 * */
	public boolean existWorkNoOther(String userId,String workNo){
		List list=userDao.find(" from User where id!='"+userId+"' and workNumber='"+workNo+"'" );
		return list != null && !list.isEmpty();
	}


	/////////////////////////////////////////////////////

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
}
